package com.test.controller;

import cn.hutool.core.util.StrUtil;
import com.test.common.AuthAccess;
import com.test.common.Result;
import com.test.mapper.UserMapper;
import com.test.mapper.WebMapper;
import com.test.pojo.User;
import com.test.service.impl.UserService;
import com.test.service.impl.WebService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Struct;

/**
 * @author 宋建华
 * @return
 * @date 2023-12-29
 */
@RestController
public class WebController {
    @Autowired
    WebService webService;
    @AuthAccess
    @GetMapping("/")
    public Result hello(){
        return Result.success("成功");
    }
    @PostMapping("/login")

    public Result login(@RequestBody User user){
        if(StrUtil.isBlank(user.getUsername()) || StrUtil.isBlank(user.getPassword())){
            return Result.error("输入有误");
        }
        user = webService.login(user);
        return Result.success(user);
    }
    @AuthAccess
    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        if (StrUtil.isBlank(user.getUsername()) || StrUtil.isBlank(user.getPassword())) {
            return Result.error("数据输入不合法");
        }
        if (user.getUsername().length() > 10 || user.getPassword().length() > 20) {
            return Result.error("数据输入不合法");
        }
        user = webService.register(user);
        return Result.success(user);
    }
    @AuthAccess
    @PutMapping("/password")
    public Result password(@RequestBody User user){
        if (StrUtil.isBlank(user.getUsername()) || StrUtil.isBlank(user.getPhone())){
            return Result.error("数据不存在");
        }
        webService.resetPassword(user);
        return Result.success();
    }
}
